import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * model - 服务器上传配置
 * @date: 2019-7-5
 */
import { getResponse, createPagination, parseParameters } from 'utils/utils';
import { queryMapIdpValue } from 'cpaas-front/lib/services/api';
import { queryConfig, createConfig as _createConfig, fetchConfigDetail as _fetchConfigDetail, saveConfig as _saveConfig } from "../services/serverUploadService";
export default {
  namespace: 'serverUpload',
  state: {
    pagination: {},
    // 分页参数
    serverUploadList: [],
    // 配置列表
    serverUploadDetail: {},
    // 配置明细
    typeList: [],
    // 上传类别
    serverConfigLineList: [] // 配置明细行数据

  },
  effects: {
    // 获取初始化数据
    init: /*#__PURE__*/_regeneratorRuntime.mark(function init(_ref, _ref2) {
      var payload, call, put, lovCodes, res, typeList;
      return _regeneratorRuntime.wrap(function init$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              lovCodes = payload.lovCodes;
              _context.t0 = getResponse;
              _context.next = 6;
              return call(queryMapIdpValue, lovCodes);

            case 6:
              _context.t1 = _context.sent;
              res = (0, _context.t0)(_context.t1);
              typeList = res.typeList;
              _context.next = 11;
              return put({
                type: 'updateState',
                payload: {
                  typeList: typeList
                }
              });

            case 11:
            case "end":
              return _context.stop();
          }
        }
      }, init);
    }),
    // 获取列表
    fetchConfigList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchConfigList(_ref3, _ref4) {
      var payload, put, call, res, data;
      return _regeneratorRuntime.wrap(function fetchConfigList$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              put = _ref4.put, call = _ref4.call;
              _context2.next = 4;
              return call(queryConfig, parseParameters(payload));

            case 4:
              res = _context2.sent;
              data = getResponse(res);

              if (!data) {
                _context2.next = 9;
                break;
              }

              _context2.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  serverUploadList: data.content,
                  pagination: createPagination(data)
                }
              });

            case 9:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchConfigList);
    }),
    // 获取配置明细
    fetchConfigDetail: /*#__PURE__*/_regeneratorRuntime.mark(function fetchConfigDetail(_ref5, _ref6) {
      var payload, put, call, res, data;
      return _regeneratorRuntime.wrap(function fetchConfigDetail$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              put = _ref6.put, call = _ref6.call;
              _context3.next = 4;
              return call(_fetchConfigDetail, payload);

            case 4:
              res = _context3.sent;
              data = getResponse(res);

              if (!data) {
                _context3.next = 9;
                break;
              }

              _context3.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  serverUploadDetail: data,
                  serverConfigLineList: data.serverConfigLineList
                }
              });

            case 9:
            case "end":
              return _context3.stop();
          }
        }
      }, fetchConfigDetail);
    }),
    // 新建配置
    createConfig: /*#__PURE__*/_regeneratorRuntime.mark(function createConfig(_ref7, _ref8) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function createConfig$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call;
              _context4.next = 4;
              return call(_createConfig, payload);

            case 4:
              res = _context4.sent;
              return _context4.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, createConfig);
    }),
    // 保存配置
    saveConfig: /*#__PURE__*/_regeneratorRuntime.mark(function saveConfig(_ref9, _ref10) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function saveConfig$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call;
              _context5.next = 4;
              return call(_saveConfig, payload);

            case 4:
              res = _context5.sent;
              return _context5.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, saveConfig);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref11) {
      var payload = _ref11.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};